package com.asura;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import cn.itcast.jdbc.JdbcUtils;
import cn.itcast.jdbc.TxQueryRunner;

public class TxQueryRunnerTest {
	
	/**
	 * TxQueryRunner的子类
	 * 使用起来跟QueryRunner差不多
	 * 区别是此类支持事务，底层使用jdbcutils来获取连接
	 * 
	 * QueryRunner3个方法
	 * update--->insert、update、delete
	 * query--->select
	 * batch--->批处理
	 * 
	 * @author 礼逊
	 * @throws SQLException 
	 */
	@Test
	public void testUpdate() throws SQLException{
		String sql = "insert into t_person(pid,pname,age,sex) values(?,?,?,?)";
		Object[] params = {"1","asura",1,"男"};
		
		QueryRunner qr = new TxQueryRunner();
		qr.update(sql,params);
	}
	@Test
	public void testUpdate2() throws SQLException{
		try {
			JdbcUtils.beginTransaction();
			String sql = "insert into t_person(pid,pname,age,sex) values(?,?,?,?)";
			QueryRunner qr = new TxQueryRunner();
			Object[] params = {"2","asura2",2,"女"};
			qr.update(sql,params);
			
			params = new Object[]{"3","asura3",3,"男"};
			qr.update(sql,params);
			
			JdbcUtils.commitTransaction();
		} catch (Exception e) {
			try {
				//回滚
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
	}
	/**
	 * 测试查询
	 * ResultSet
	 */
	public void testQuery(){
		
	}
	
	public void testJoin(){
		
	}
	
}
